package im.vinci.server.utils;

import im.vinci.server.common.domain.music.MusicSong;
import im.vinci.server.config.ServiceConfiguration;
import im.vinci.server.utils.apiresp.APIResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by mingjie on 16/11/8.
 */
@Component
@Import(ServiceConfiguration.class)
public class NLPServerUtil {

    private static Logger logger = LoggerFactory.getLogger(NLPServerUtil.class);

    private static String address;

    @Value("${nlp_address}")
    private void setAddress(String address) {
        NLPServerUtil.address = address;
    }

    public static MusicSong getMusic(long songId, String songType) {
        String url = address + "/song/" + songId;

        Map<String, String> map = new HashMap<String, String>();
        map.put("song_type", songType);
        String respStr = null;
        MusicSong music = null;
        try {
            respStr = WebUtils.doGet(url, map);
            if (respStr != null) {
                logger.info("get Music id={},type={}, response is {}", songId, songType, respStr);
                APIResponse<LinkedHashMap<String, String>> response = JsonUtils.decode(respStr, APIResponse.class);
                if (response != null && response.getErrcode() == 0 && response.getData() != null) {
                    LinkedHashMap<String, String> dataMap = response.getData();
                    String jsonStr = JsonUtils.encode(dataMap);
                    music = JsonUtils.decode(jsonStr, MusicSong.class);
                }
            }
        } catch (IOException e) {
            logger.error("getMusic error, musicId={}, musicType={}", songId, songType, e);
        }
        return music;
    }

    public static void main(String[] args) {
        MusicSong music = getMusic(122832, "xiami");
        System.out.println(music);
        if (music != null) {
            System.out.println(music.getSongName());
            System.out.println(music.getTags());
            System.out.println(music.getTagCounts());
        }
    }

}